package com.rk.kbk.controller;

import com.rk.kbk.common.JsonResp;
import com.rk.kbk.dao.PunishSettingDao;
import com.rk.kbk.dto.PageDto;
import com.rk.kbk.model.CallSetting;
import com.rk.kbk.model.PunishSetting;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import java.math.BigInteger;
import java.util.List;

/**
 * @PunishSettingController
 * @Controller
 * @version : Ver 1.0
 */
@CrossOrigin
@RestController
@Transactional
@RequestMapping(value="/api/punishSetting")
public class PunishSettingController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private PunishSettingDao punishSettingDao;
    @PersistenceContext
    private EntityManager entityManager;

    /**
     * @添加/修改
     * @param punishSetting
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/addOrUpdatePunishSetting",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp addOrUpdatePunishSetting(@RequestBody PunishSetting punishSetting){
        log.debug("添加/修改");
        punishSettingDao.save(punishSetting);
        return JsonResp.ok();
    }

    /**
     * @批量添加/修改
     * @param punishSettings
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/addOrUpdateSomePunishSetting",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp addOrUpdateSomePunishSetting(@RequestBody List<PunishSetting> punishSettings){
        log.debug("批量添加/修改");
        punishSettingDao.save(punishSettings);
        return JsonResp.ok();
    }

    /**
     * @获取详情-ByPunishSettingId
     * @param punishSettingId
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/punishSettingDetails",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findPunishSettingDetailsByPunishSettingId(Long punishSettingId) {
        log.debug("获取详情-ByPunishSettingId");
        return JsonResp.ok(punishSettingDao.findOne(punishSettingId));
    }

    /**
     * @分页查询(jpa方式)
     * @param pageNum 第几页
     * @param pageSize 每页的条数
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/jpa/page",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findPagePunishSettingJpa( Integer pageNum, Integer pageSize) {
        log.debug("分页查询(jpa方式)");
        //多条件排序Sort
        //Sort sort = new Sort(Sort.Direction.DESC, "createdate").and(new Sort(Sort.Direction.AES, "id"));
        //排序Sort
        Sort sort = new Sort(Sort.Direction.DESC, "gmtDatetime");
        Pageable pageable = new PageRequest(pageNum-1, pageSize, sort);
        Page page = null;
            page = punishSettingDao.findAll(pageable); //无条件分页
        return JsonResp.ok(new PageDto(page));
    }

    /**
     * @删除-one(永久)
     * @param punishSettingId
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/deletePunishSettingOne",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp deletePunishSettingOne(Long punishSettingId) {
        log.debug("删除-one(永久)");
        punishSettingDao.delete(punishSettingId);
        return JsonResp.ok();
    }

    /**
     * @批量删除(永久)
     * @param punishSettings
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/deleteSomePunishSetting",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp deleteSomePunishSetting(@RequestBody List<PunishSetting> punishSettings) {
        log.debug("批量删除(永久)");
        punishSettingDao.delete(punishSettings);
        return JsonResp.ok();
    }

    /**
     * @修改状态
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/updateStatus",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp updateStatus(Long id) {
        log.debug("修改状态");
        PunishSetting p =punishSettingDao.findOne(id);
        if(p.getStatus()==0){
            p.setStatus(1);
        }else{
            p.setStatus(0);
        }
        punishSettingDao.save(p);
        return JsonResp.ok();
    }

    /**
     * @查所有处罚
     * @param userType
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/allPunish",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp allPunish(Integer userType,Integer punishType){
        log.debug("查所有处罚");
        return JsonResp.ok(punishSettingDao.findByUserTypeAndPunishType(userType,punishType));
    }
}
